package com.team.game.Game.Boses;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PathMeasure;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;

import com.team.game.framework.Abstract.Boses;
import com.team.game.framework.impl.MainActivity;
import com.team.game.framework.impl.SurfaceThread;

import java.io.IOException;
import java.io.InputStream;

import static com.team.game.framework.impl.Scale_X_Y.Scale_X;
import static com.team.game.framework.impl.Scale_X_Y.Scale_Y;

/**
 * Боссы - любые уникальные крипы для которых нужно определить дополнитеные методы
 * Для создания босса нужно определить класс унаследованный от Boses
 * Этот класс должен находиться в пакете Boses
 */

public class Boss1 extends Boses {

    private int Num_Mob;
    private Bitmap Boss_Machula;
    private boolean toMachula = false;
    private float distance1;
    private float distance;

    public Boss1(PathMeasure[] pathMeasure) {
        rectSrc = new Rect(0, 0, 80, 80);
        rectDst = new RectF(0, 0, 0, 0);
        start_hp = health = 100;
        speed = (float) (0.5 * (Scale_X + Scale_Y) / 2);
        InputStream inputStream = null;
        try {
            inputStream = MainActivity.manager.open("Picture/Boss.png");
            Boss = (BitmapFactory.decodeStream(inputStream));
            inputStream = MainActivity.manager.open("Picture/Boss_Machula.png");
            Boss_Machula = (BitmapFactory.decodeStream(inputStream));
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.pathMeasure = pathMeasure;
        destenation = Boss.getWidth() / 3 * Scale_X;
        sprWidth = Boss.getWidth() / 4;
        sprHeight = Boss.getHeight() / 4;
        matrix = new Matrix();
        pathMeasure[1].getMatrix(pathMeasure[1].getLength(), matrix, PathMeasure.POSITION_MATRIX_FLAG);
    }


    @Override
    public void drawSprite(Canvas canvas) {
        if (!deadSearch) {
            if (!toMachula)
                canvas.drawBitmap(Boss, rectSrc, rectDst, null);
            else
                canvas.drawBitmap(Boss_Machula, rectSrc, rectDst, null);
        }
    }

    @Override
    public void updateSprite() {
        if (!deadSearch) {
            rectDst.setEmpty();
            pathMeasure[1].getMatrix(speed * frame, matrix, PathMeasure.POSITION_MATRIX_FLAG);
            matrix.mapRect(rectDst);
            rectDst.bottom += destenation;
            rectDst.right += destenation;
            frame += 187.5 * SurfaceThread.deltaTime;
            fCounter += SurfaceThread.deltaTime;
            if (fCounter > 0.1) {
                curFrame++;
                if (curFrame >= 4) {
                    curFrame = 0;
                }
                rectSrc.left = curFrame * sprWidth;
                rectSrc.right = rectSrc.left + sprWidth;
                fCounter -= 0.1;
            }
        }
    }

    @Override
    public void restart() {
        if (deadSearch) {
            frame = 0;
            health = start_hp;
            dead = 0;
            deadSearch = false;
        }
    }

    @Override
    public RectF getRect(int mob) {
        return rectDst;
    }

    @Override
    public void dec(int mob, float kick) {
        health -= kick;
        if (health < 0) {
            if (!toMachula) {
                health = 1000;
                destenation = Boss.getWidth() / 2 * Scale_X;
                toMachula = true;
            } else {
                deadSearch = true;
            }
        }
    }

    private PointF pointF = new PointF();

    public PointF getAllPosition(float x, float y, float init, float radius) {
        Num_Mob = -1;
        distance1 = init;
        if (!deadSearch) {
            distance = (float) (Math.sqrt(Math.pow((rectDst.centerX() - x), 2.0) +
                    Math.pow(rectDst.centerY() - y, 2.0)));
            if (distance <= distance1 && distance > radius) {
                distance1 = distance;
                Num_Mob = 100;
                pointF.set(Num_Mob, distance1);
                return pointF;
            }
        }
        pointF.set(Num_Mob, distance1);
        return pointF;
    }

    @Override
    public void dispose() {
        Boss.recycle();
    }
}

